package com.ysd.lis.service.samsto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.mapper.samsto.SamstoSpecboxModelMapper;
import com.ysd.lis.entity.samsto.SamstoSpecboxModel;
import com.ysd.lis.service.samsto.SamstoSpecboxModelService;
import com.ysd.lis.request.samsto.SamstoSpecboxModelParam;
import org.springframework.stereotype.Service;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ysd.common.Result;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.lis.entity.SysUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标本盒型号表(SamstoSpecboxModel)表服务实现类
 *
 * @author leiming
 * @since 2024-10-26 09:14:07
 */
@Service("samstoSpecboxModelService")
public class SamstoSpecboxModelServiceImpl extends ServiceImpl<SamstoSpecboxModelMapper, SamstoSpecboxModel> implements SamstoSpecboxModelService {

    private MPJLambdaWrapper<SamstoSpecboxModel> initWrapper(){
        MPJLambdaWrapper<SamstoSpecboxModel> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        		//ID 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getId, SamstoSpecboxModel::getId);
		//型号 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getSpecboxModel, SamstoSpecboxModel::getSpecboxModel);
		//适用试管范围 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getTubeTypesRange, SamstoSpecboxModel::getTubeTypesRange);
		//适用温度 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getTempRange, SamstoSpecboxModel::getTempRange);
		//阵列 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getSpecboxArray, SamstoSpecboxModel::getSpecboxArray);
		//仓位序列 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getOrderNumber, SamstoSpecboxModel::getOrderNumber);
		//尺寸 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getSpecboxSize, SamstoSpecboxModel::getSpecboxSize);
		//用途 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getPurpose, SamstoSpecboxModel::getPurpose);
		//机构 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getOrgId, SamstoSpecboxModel::getOrgId);
		//状态 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getStatus, SamstoSpecboxModel::getStatus);
		//所属科室 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getDeptName, SamstoSpecboxModel::getDeptName);
		//删除标记 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getDelFlag, SamstoSpecboxModel::getDelFlag);
		//创建人 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getCreator, SamstoSpecboxModel::getCreator);
		//创建时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getCreateTime, SamstoSpecboxModel::getCreateTime);
		//修改人 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getEditor, SamstoSpecboxModel::getEditor);
		//修改时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getEditTime, SamstoSpecboxModel::getEditTime);
		//删除时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxModel::getDelTime, SamstoSpecboxModel::getDelTime);
        return lambdaQueryWrapper;
    }

    @Override
    public Result findPageList(SamstoSpecboxModelParam pageParam) {
        return this.findListByGId(pageParam);
    }

    @Override
    public Result findListByGId(SamstoSpecboxModelParam pageParam) {
        Page<SamstoSpecboxModel> pageList=null;
        LambdaQueryWrapper<SamstoSpecboxModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoSpecboxModel> page = new Page<SamstoSpecboxModel>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        if(StringUtils.isNotBlank(pageParam.getDeptName())){
            lambdaQueryWrapper.eq(SamstoSpecboxModel::getDeptName, pageParam.getDeptName());
        }
        lambdaQueryWrapper.eq(SamstoSpecboxModel::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(SamstoSpecboxModel::getSpecboxModel, pageParam.getKeywords()));
        }
        lambdaQueryWrapper.orderByAsc(SamstoSpecboxModel::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoSpecboxModel::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result getInfoById(String id) {
        SamstoSpecboxModel SamstoSpecboxModel=this.baseMapper.selectById(id);
        return Result.succ(1, "获取信息成功", SamstoSpecboxModel);
    }

    @Override
    public Result add(SamstoSpecboxModelParam param) {
        SamstoSpecboxModel info=new SamstoSpecboxModel();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setCreator(sysUser.getId());
                param.setOrgId(sysUser.getOrgId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setCreateTime(new Date());
        BeanUtil.copyProperties(param,info);
        info.setDelFlag(0);
        info.setStatus(1);
        this.baseMapper.insert(info);
        return Result.succ(1, "添加信息成功", null);
    }

    @Override
    public Result edit(SamstoSpecboxModelParam param) {
        Map returnMap = new HashMap();
        SamstoSpecboxModel info=new SamstoSpecboxModel();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setEditor(sysUser.getId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setEditTime(new Date());
        BeanUtil.copyProperties(param,info);
        this.baseMapper.updateById(info);
        returnMap.put("id", param.getId());
        return Result.succ(1, "修改信息成功", returnMap);
    }

    @Override
    public Result delete(List<String> ids) {
        Result result;
        try {
            boolean b = this.removeByIds(ids);
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result updelete(List<String> ids) {
        Result result;
        LambdaUpdateWrapper<SamstoSpecboxModel> lambdaUpdateWrapper = new UpdateWrapper<SamstoSpecboxModel>().lambda();
        // set 表示要更新的字段， eq 表示  WHERE 条件
        lambdaUpdateWrapper.set(SamstoSpecboxModel::getDelFlag,1)
                .in(SamstoSpecboxModel::getId, ids);
        this.baseMapper.update(new SamstoSpecboxModel(), lambdaUpdateWrapper);

        try {
            int b= this.baseMapper.update(new SamstoSpecboxModel(), lambdaUpdateWrapper);
            if (b>0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }
    
    @Transactional
    @Override
    public Result saveOrUpdateList(SamstoSpecboxModelParam param) {
        Result result;
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
               return Result.fail(304, "请重新登陆", "");
            }
            for (SamstoSpecboxModel info : param.getInfoList()) {
                if(StringUtils.isNotBlank(info.getId())){
                    info.setEditor(sysUser.getId());
                    info.setEditTime(new Date());
                    this.baseMapper.updateById(info);
                }else{
                    info.setCreator(sysUser.getId());
                    info.setOrgId(sysUser.getOrgId());
                    info.setCreateTime(new Date());
                    info.setDelFlag(0);
                    info.setStatus(1);
                    this.baseMapper.insert(info);
                }
            }
            Boolean b= this.saveOrUpdateBatch(param.getInfoList());
            if (b) {
                result = Result.succ(1, "保存成功", null);
            } else {
                result = Result.fail(400, "保存失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "保存失败", "");
        }
        return result;
    }
}

